package com.micookie.v2ex.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.micookie.v2ex.R;
import com.micookie.v2ex.api.APIConfig;
import com.micookie.v2ex.ui.adapter.FragPagerAdapter;
import com.micookie.v2ex.ui.base.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2017-01-03.
 */

public class TopicListTabsFragment extends BaseFragment {
    @BindView(R.id.tablayout)
    TabLayout tabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private FragPagerAdapter fragPagerAdapter;
    ArrayList<Fragment> fragmentList;
    ArrayList<String> tabList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_topic_list_tabs, container, false);
    }

    @Override
    public void initData() {
        super.initData();
        createData();
    }

    @Override
    public void initView() {
        super.initView();

        fragPagerAdapter = new FragPagerAdapter(getFragmentManager(), fragmentList, tabList);
        viewPager.setAdapter(fragPagerAdapter);
        viewPager.setOffscreenPageLimit(1); //设置pager数量缓存

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//设置tabLayout可以滑动
    }


    @Override
    public void initListener() {
        super.initListener();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    /**
     * 初始化默认tabs数据
     */
    private void createData() {
        fragmentList = new ArrayList<>();
        tabList = new ArrayList<>();

        for (String[] tab : tabArray) {
            tabList.add(tab[0]);
            fragmentList.add(TopicListFragment.startFragment(tab[1]));
        }
    }


    public static final String[][] tabArray = {
            {"程序员", APIConfig.getBaseUrl() + "/go/programmer"},
            {"全部", APIConfig.getBaseUrl() + "/?tab=all"},
            {"最热", APIConfig.getBaseUrl() + "/?tab=hot"},
            {"技术", APIConfig.getBaseUrl() + "/?tab=tech"},
            {"创意", APIConfig.getBaseUrl() + "/?tab=creative"},
            {"好玩", APIConfig.getBaseUrl() + "/?tab=play"},
            {"Apple", APIConfig.getBaseUrl() + "/?tab=apple"},
            {"酷工作", APIConfig.getBaseUrl() + "/?tab=jobs"},
            {"交易", APIConfig.getBaseUrl() + "/?tab=deals"},
            {"城市", APIConfig.getBaseUrl() + "/?tab=city"},
            {"问与答", APIConfig.getBaseUrl() + "/?tab=qna"}
    };
}
